<?php

namespace App\Modules\Health\Http\Controllers\Admin;

use App\Modules\Health\Repositories\HealthPlan;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HealthPlanController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HealthPlan(['disease']), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('member_id');
            $grid->column('disease_id');
            $grid->column('disease.name', '病症名称');
            $grid->column('acu_name');
            $grid->column('acu_num');
            $grid->column('acu_img')->image(' ', 50, 50);
            $grid->column('finish_day');
            $grid->column('day');
            $grid->column('status')->using(['进行中', '已完成'])->label(['danger', 'primary']);
            $grid->column('last_finish_time')->display(function ($v){
                return $v > 0 ? date('Y-m-d H:i:s', $v) : '';
            });

            $grid->column('mini_type')->display(function ($v) use($map) {
                return $v ? $map[$v] : '';
            });
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) use($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('member_id')->width(2);
                $filter->equal('disease_id')->width(2);

            });

            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HealthPlan(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('disease_id');
            $show->field('acu_name');
            $show->field('acu_num');
            $show->field('acu_img');
            $show->field('finish_day');
            $show->field('day');
            $show->field('status');
            $show->field('last_finish_time');
            $show->field('log');
            $show->field('mini_type');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HealthPlan(), function (Form $form) {
            $form->display('id');
            $form->text('member_id');
            $form->text('disease_id');
            $form->text('acu_name');
            $form->text('acu_num');
            $form->text('acu_img');
            $form->text('finish_day');
            $form->text('day');
            $form->text('status');
            $form->text('last_finish_time');
            $form->text('log');
            $form->text('mini_type');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
